﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

using AnOtherBlog.Core;


namespace AnOtherBlog.AJAX.Admin.Posts
{
	public partial class CategoryEdit : AnOtherBlog.Core.Web.AdminBasePage
	{
		
		protected void Page_Load(object sender, EventArgs e)
		{
			//base.LblMsg = this.lblMsg;

			if (!Page.IsPostBack)
			{
				string sId = Util.GetParameter("id");
				if (!string.IsNullOrEmpty(sId))
				{
					BindCategory(new Guid(sId));
				}
				BindCategoryList();
			}

			btnCancel.Click += new EventHandler(btnCancel_Click);
			btnSave.Click += new EventHandler(btnSave_Click);

			gvCategory.PageIndexChanging += new GridViewPageEventHandler(gvCategory_PageIndexChanging);
			gvCategory.RowDeleting += new GridViewDeleteEventHandler(gvCategory_RowDeleting);
			gvCategory.RowEditing += new GridViewEditEventHandler(gvCategory_RowEditing);
		}

		/// <summary>
		/// Triggered after Page Number command is pressed 
		/// </summary>
		/// <param name="sender">Source of the event</param>
		/// <param name="e">The <see cref="System.GridViewPageEventArgs"/> instance containing the event data.</param>
		void gvCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
		{
			if (gvCategory.EditIndex != -1)
			{
				e.Cancel = true;
				MsgSet("Debe cancelar la modificaci&oacute;n antes de cambiar de p&aacute;gina");
			}
			else
			{
				gvCategory.PageIndex = e.NewPageIndex;
				BindCategoryList();
			}
		}

		/// <summary>
		/// Triggered after Delete command is pressed
		/// </summary>
		/// <param name="sender">Source of the event</param>
		/// <param name="e">The <see cref="System.GridViewDeleteEventArgs"/> instance containing the event data.</param>
		void gvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
		{
			MsgSetCritical("Por ahora no se permite eliminar categorías");
		}

		/// <summary>
		/// Triggered after Edit command is pressed
		/// </summary>
		/// <param name="sender">Source of the event</param>
		/// <param name="e">The <see cref="System.GridViewEditEventArgs"/> instance containing the event data.</param>
		void gvCategory_RowEditing(object sender, GridViewEditEventArgs e)
		{
			e.Cancel = true;
			GridViewRow row = gvCategory.Rows[e.NewEditIndex];
			Guid id = new Guid(((Label) row.FindControl("lblCategoryID")).Text);
			Response.Redirect(Request.Path + "?id=" + id.ToString(), false);
		}


		/// <summary>
		/// Triggered after Click
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		void btnCancel_Click(object sender, EventArgs e)
		{
			Response.Redirect(Request.Path, false);
		}

		/// <summary>
		/// Triggered after Click
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				if (!Page.IsValid)
					throw new InvalidOperationException("Alguna de las validaciones no se cumple.");

				Category category = null;
				string sId = Util.GetParameter("id");
				if (!string.IsNullOrEmpty(sId))
				{
					category = Category.Load(new Guid(sId));
				}
				else
				{
					category = new Category();
				}

				category.CategoryName = txtCategoryName.Text;
				category.Description = txtDescription.Text;

				category.Save();

				Blog.Current.ReLoadCategories();

				BindCategoryList();
				MsgSet("Categoría Grabada Satisfactoriamente");
			}
			catch (Exception ex)
			{
				MsgSetCritical(ex.Message);
			}
		}

		/// <summary>
		/// Enlaza los datos de una categoría con los controles de edición
		/// </summary>
		/// <param name="Id">Identificador de la categoría</param>
		private void BindCategory(Guid Id)
		{
			Category category = Category.Load(Id);
			txtCategoryName.Text = category.CategoryName;
			txtDescription.Text = category.Description;
		}

		/// <summary>
		/// Enlaza la lista de categorías con el gridview
		/// </summary>
		private void BindCategoryList()
		{
			gvCategory.DataSource = Blog.Current.Categories;
			gvCategory.DataBind();
		}

	}
}
